﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DAL.Entities;

namespace DAL.EntitiesDAL
{
    public class UserDAL: BaseDAL, UTL.IBaseDAL
    {
        #region Implement

        public int Count()
        {
            throw new NotImplementedException();
        }

        public DataTable Select()
        {
            return ExecuteQuery("sp_SelectAll_User");
        }

        public DataTable Select(object obj)
        {
            throw new NotImplementedException();
        }

        public object GetByKey(object key)
        {
            var sql = @"select * from tb_User where Ten = '{0}'";
            sql = String.Format(sql, key);
            var tbl = ExecuteQuery(sql);

            if (tbl.Rows.Count > 0)
            {
                var usr = new Entities.User();
                usr.Ten = tbl.Rows[0]["Ten"] + "";
                usr.Matkhau = tbl.Rows[0]["Matkhau"] + "";
                usr.Hoten = tbl.Rows[0]["Hoten"] + "";
                usr.VaitroID = Convert.ToInt32(tbl.Rows[0]["VaitroID"] + "");
                usr.Images = tbl.Rows[0]["Images"] + "";
                return usr;
            }
            else return null;
        }

        public bool Delete(string id)
        {
            throw new NotImplementedException();
        }

        public bool Delete(long id)
        {
            var sql = "sp_Delete_User {0}";
            sql = string.Format(sql, id);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Insert(object obj)
        {
            var o = (User)obj;
            var sql = "set dateformat dmy; execute sp_Insert_User '{0}', '{1}', N'{2}', N'{3}', '{4}' ";
            sql = string.Format(sql, o.Ten, o.Matkhau, o.Hoten, o.VaitroID, o.Images);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Update(object obj)
        {
            var o = (User)obj;
            var sql = "set dateformat dmy; execute sp_Update_User '{0}', '{1}', '{2}', N'{3}', '{4}', '{5}' ";
            sql = string.Format(sql, o.Id, o.Ten, o.Matkhau, o.Hoten, o.VaitroID, o.Images);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Update_MatKhau(object obj)
        {
            var o = (User)obj;
            var sql = "set dateformat dmy; execute sp_Update_MatKhau '{0}', '{1}' ";
            sql = string.Format(sql, o.Ten, o.Matkhau);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public DataTable Search(string name)
        {
            var sql = @"select * from tb_User where Ten like N'%{0}%' ";
            sql = string.Format(sql, name);
            return ExecuteQuery(sql);
        }

        public DataTable Search(DAL.Entities.User o)
        {
            string sql;
            if (o.Ten!= "")
                sql = @"Select * from tb_User Where Ten like '%" + o.Ten + "%' and Matkhau like '%" + o.Matkhau + "%' and VaitroID like '%" + o.VaitroID + "%' ";
            else
                sql = @"Select * from tb_User Where Matkhau like N'%" + o.Matkhau + "%' and Hoten like N'%" + o.Hoten + "%' and VaitroID like N'%" + o.VaitroID + "%' ";
            return ExecuteQuery(sql);
        }

        #endregion  
    }
}
